package com.j2110.controller;

import com.j2110.pojo.Dept;
import com.j2110.service.DeptService;
import com.j2110.utils.Result;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {

    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptService service = new DeptService();
        List<Dept> list = service.getDepts();
        req.setAttribute("DEPT_LIST", list);
        //转发
        req.getRequestDispatcher("/WEB-INF/pages/dept.jsp").forward(req, resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据
        Map<String, String[]> map = req.getParameterMap();
        //将数据封装到Dept对象中
        Dept dept = new Dept();
        try {
            BeanUtils.populate(dept,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用DeptServlet中的execute方法添加数据
        DeptService service = new DeptService();
        Result re = service.execute(dept);
        if (re.isSuccess()){
            req.getRequestDispatcher("/dept/index").forward(req, resp);
        }else {
            resp.sendRedirect(req.getContextPath() + "/WEB-INF/pages/dept.jsp");
        }
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的id信息
        String id = req.getParameter("id");
        //调用DeptService的删除方法
        DeptService servlet = new DeptService();
        Result result = servlet.delete(id);
        if (result.isSuccess()){
            // 删除成功就转发到查询
            req.getRequestDispatcher("dept/index").forward(req, resp);
        }else {
            resp.sendRedirect(req.getContextPath() + "/WEB-INF/pages/dept.jsp");
        }
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前行数据的id
        String id = req.getParameter("id");
        //调用业务层方法查询部门信息
        DeptService service = new DeptService();
        Result result = service.query(id);
        if (result.isSuccess()) {
            req.setAttribute("list", result.getData());
            req.getRequestDispatcher("/WEB-INF/pages/dept_edit.jsp").forward(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/dept/index");
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据
        Map<String, String[]> map = req.getParameterMap();
        //封装数据
        Dept dept = new Dept();
        try {
            BeanUtils.populate(dept, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用DeptService的update方法
        DeptService service = new DeptService();
        Result result = service.update(dept);
        if (result.isSuccess()){
            req.getRequestDispatcher("dept/index").forward(req, resp);
        }else {
            resp.sendRedirect(req.getContextPath() + "/WEB-INF/pages/dept.jsp");
        }
    }

    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search");
        DeptService service = new DeptService();
        Result re = service.query(search);
        Result res = service.queryByName(search);
        if (re.isSuccess()){
            req.setAttribute("DEPT_LIST", re.getData());
            req.getRequestDispatcher("/WEB-INF/pages/dept.jsp").forward(req, resp);
        }else if (res.isSuccess()){
            req.setAttribute("DEPT_LIST", res.getData());
            req.getRequestDispatcher("/WEB-INF/pages/dept.jsp").forward(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/dept/index");
        }
    }
}
